<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :permission="permissionList"
        :table-loading="tableLoading"
        :data="tableData"
        :option="tableOption"
        @on-load="getList"
        @row-save="handleSave"
        @row-update="handleUpdate"
        @row-del="handleDel"
        @size-change="sizeChange"
        @current-change="currentChange"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        :before-open="beforeOpen"
        :before-close="beforeClose"
      >
        <template slot="menu" slot-scope="scope">
          <el-button v-if="permissions.sys_dict_add" icon="el-icon-menu" type="text" size="small" @click="handleItem(scope.row)">字典项</el-button>
        </template>
      </avue-crud>
    </basic-container>

    <el-dialog :visible.sync="dialogFormVisible" title="字典项管理" width="90%">
      <avue-crud
        ref="itemCrud"
        :page.sync="itemPage"
        :permission="permissionList"
        :data="tableItemData"
        :option="tableItemOption"
        @row-save="handleItemSave"
        @row-update="handleItemUpdate"
        @row-del="handleItemDel"
        @size-change="itemSizeChange"
        @current-change="itemCurrentChange"
        :before-open="itemBeforeOpen"
        :before-close="itemBeforeClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import {tableOption, tableItemOption} from "./dict.js";
import {fetchList, addObj, putObj, delObj, fetchItemList, addItemObj, putItemObj, delItemObj} from "@/api/admin/dict";
import {mapGetters} from "vuex";

export default {
  name: "Dict",
  data() {
    return {
      //字典
      searchForm:{},
      tableLoading: false,
      tableOption: tableOption,
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 ,// 每页显示多少条
      },
      //向字典项传值
      dictType: undefined,
      dictId: undefined,

      //字典项
      itemForm: {},
      dialogFormVisible: false,
      tableItemOption: tableItemOption,
      tableItemData: [],
      itemPage: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      }
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.sys_dict_add, false),
        delBtn: this.vaildData(this.permissions.sys_dict_del, false),
        editBtn: this.vaildData(this.permissions.sys_dict_edit, false)
      };
    }
  },
  methods: {
    //======字典表格相关=====
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
            descs: "create_time"
          },
          params,
          this.searchForm
        )
      ).then(response => {
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      });
    },
    handleSave: function(row, done) {
      addObj(row).then(() => {
        this.$message.success("添加成功");
        this.getList(this.page);
        done();
      });
    },
    handleUpdate: function(row, index, done) {
      putObj(row).then(() => {
        this.$message.success("修改成功");
        this.getList(this.page);
        done();
      });
    },
    handleDel: function(row) {
      this.$confirm('是否确认删除数据类型为"' + row.type + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row);
        })
        .then(() => {
          this.getList(this.page);
          this.$message.success("删除成功");
        });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },
    beforeOpen: function(show, type) {
      show();
    },
    beforeClose: function(done, type) {
      done();
    },

    handleItem: function(row) {
      this.dictId = row.id;
      this.dictType = row.type;
      this.getDictItemList(row.id);
    },

    //======字典项表格相关=====
    getDictItemList(dictId) {
      this.dialogFormVisible = true;
      fetchItemList(
        Object.assign(
          {
            current: this.itemPage.currentPage,
            size: this.itemPage.pageSize
          },
          {
            dictId: dictId
          }
        )
      ).then(response => {
        this.tableItemData = response.data.data.records;
        this.itemPage.total = response.data.data.total;
      });
    },

    handleItemSave: function(row, done) {
      addItemObj(row).then(() => {
        this.$message.success("添加成功");
        this.getDictItemList(this.dictId);
        done();
      });
    },
    handleItemUpdate: function(row, index, done) {
      putItemObj(row).then(() => {
        this.$message.success("修改成功");
        this.getDictItemList(this.dictId);
        done();
      });
    },
    handleItemDel: function(row) {
      this.$confirm('是否确认删除数据为"' + row.label + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delItemObj(row.id);
        })
        .then(() => {
          this.getDictItemList(this.dictId);
          this.$message.success("删除成功");
        })
        .catch(function() {});
    },
    itemSizeChange(pageSize) {
      this.itemPage.pageSize = pageSize;
      this.getDictItemList();
    },
    itemCurrentChange(current) {
      this.itemPage.currentPage = current;
      this.getDictItemList();
    },
    itemBeforeOpen: function(done, type) {
      if(type=="add"){
        this.$set(this.$refs.itemCrud.tableForm,"dictId",this.dictId)
        this.$set(this.$refs['itemCrud'].tableForm,"dictType",this.dictType)
      }
      if(type=="edit"){

      }
      done();
    },
    itemBeforeClose: function(done, type) {
      done();
    }
  }
};
</script>

<style lang="scss" scoped></style>
